//
//  HOTSettingViewController.m
//  Genpara
//
//  Created by AndyChang.zhang on 14-9-26.
//  Copyright (c) 2014年 zhangchh. All rights reserved.
//

#import "HOTSettingViewController.h"
#import "HOTSettingListCell.h"
#import "HOTNotificationSettingViewController.h"
#import "HOTResetPwdViewController.h"
#import "HOTCommWebViewController.h"
#import <MessageUI/MessageUI.h>
#import "HOTAccountViewController.h"

@interface HOTSettingViewController () <UITableViewDataSource, UITableViewDelegate, MFMailComposeViewControllerDelegate, UINavigationControllerDelegate> {
    
    NSArray *_titleArr;
    NSArray *_urlArr;
    MFMailComposeViewController *mailVc;
}

@end

@implementation HOTSettingViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setNaviTitle:@"設定"];
    
    _titleArr = @[@"プッシュ通知", @"アカウント",@"現場パラダイスとは",@"お問い合わせ",@"利用規約",@"特定商取引の表示",];
    _urlArr = @[@"プッシュ通知", @"アカウント",@"new/04.html",@"new/07.html",@"new/01.html",@"new/02.html"];
}

#pragma mark - UItableView DataSource
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellId = @"cellID";
    HOTSettingListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (cell == nil) {
        
        UINib *nib = [UINib nibWithNibName:XIB(@"HOTSettingListCell") bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:cellId];
        
        cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    }
    
    NSString *titleStr = [_titleArr objectAtIndex:indexPath.row];
    cell.titleLabel.text = titleStr;
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _titleArr.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 60;
}

#pragma mark - UITableView Delegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    _urlArr = @[@"プッシュ通知", @"アカウント",@"new/04.html",@"new/07.html",@"パスワード再設定",@"new/01.html",@"new/02.html",@"new/08.html",@"new/09.html", @"new/03.html"];
//    _titleArr = @[@"プッシュ通知", @"アカウント",@"現場パラダイスとは",@"お問い合わせ",@"パスワード再設定",@"利用規約",@"特定商取引の表示",@"資金決済法に基づく表示",@"運営会社",@"プライバシーポリシー",];
    
    NSString *title = [_titleArr objectAtIndex:indexPath.row];
    
    if (indexPath.row == 0) {
        //通知设定
        HOTNotificationSettingViewController *nsvc = [[HOTNotificationSettingViewController alloc] initWithNibName:@"HOTNotificationSettingViewController" bundle:nil];
        
        [self.navigationController pushViewController:nsvc animated:YES];
    } else if (indexPath.row == 1) {
        
        //アカウント
        HOTAccountViewController *avc = [[HOTAccountViewController alloc] initWithNibName:@"HOTAccountViewController" bundle:nil];
        [self.navigationController pushViewController:avc animated:YES];
    }
//    else if (indexPath.row == 4) {
//        
//        //重设密码
//        HOTResetPwdViewController *rpvc = [[HOTResetPwdViewController alloc] initWithNibName:@"HOTResetPwdViewController" bundle:nil];
//        [self.navigationController pushViewController:rpvc animated:YES];
//    }
    else if (indexPath.row == 3) {
        
        //お問い合わせ
        if ([MFMailComposeViewController canSendMail]) {
            NSLog(@"can send mail");
        }
        
        mailVc = [[MFMailComposeViewController alloc] init];
        
        if (!mailVc) {
            //没有建立邮箱账号
            return;
        }
        mailVc.mailComposeDelegate = self;
        [mailVc setSubject:@"【現パラ】お問い合わせ"];
        [mailVc setToRecipients:@[@"info@gen-para.com"]];
        [self presentViewController:mailVc animated:YES completion:nil];
        
    } else {
        
        //共同web页表示
        HOTCommWebViewController *cwvc = [[HOTCommWebViewController alloc] initWithNibName:@"HOTCommWebViewController" bundle:nil url:[NSString stringWithFormat:@"%@%@",HOT_URL_HOST, [_urlArr objectAtIndex:indexPath.row]]];
        [cwvc setNaviTitle:title];
        [self.navigationController pushViewController:cwvc animated:YES];
    }
}

-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {

    [controller dismissViewControllerAnimated:YES completion:nil];
    
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)dealloc {
    
    mailVc.mailComposeDelegate = nil;
}

@end
